package com.mizhiquan.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Shane on 2017/7/13.
 */
public class CheckUtil {

    /**
     * 检测密码的正确性
     * @param pass
     * @return
     */
    public static boolean checkPass(String pass){
        //匹配顺增和顺降的密码，6位及以上
        Pattern pa = Pattern.compile("(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5,}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5,})\\d");
        Matcher ma = pa.matcher(pass);
        if(ma.matches())
            return false;
        //匹配任意可见字符，6-18位//old:"^[0-9a-zA-Z\\-\\[\\]~_/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}+.*$]{6,18}"
        if(!pass.matches("[^\\s]{6,18}"))
            return false;
        if(pass.matches("^.*(.)\\1{2,}+.*$"))
            return false;
        else
            return true;
    }

    /**
     * 检测邮箱正确性
     * @param email
     * @return
     */
    public static boolean checkEmail(String email){
        if(StringUtils.isBlank(email))
            return false;
        Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 检测手机号的正确性
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone){
        if(StringUtils.isBlank(phone))
            return false;
        if(!phone.matches("^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$"))
            return false;
        else
            return true;
    }

    /**
     * 检测给出的时间是否超时
     * @param date
     * @param minutes
     * @return
     */
    public static boolean isTimeOut(Date date, Integer minutes){
        Date nowdate = new Date();
        long timeOne=date.getTime();
        long timeTwo=nowdate.getTime();
        long minute=(timeTwo-timeOne)/(1000*60);
        if(minute>=minutes.longValue())
        {
            return true;
        }else
            return false;

    }


    public static void main(String[] args){
        System.out.println(checkPass("1234567-"));
        System.out.println(checkEmail("w_ewr3@vip.qq.com"));
        Date ndate = null;
        try {
            ndate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-07-13 23:35:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(isTimeOut(ndate,1));
    }
}
